package com.hznc.wcs.convert.bpm;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.map.MapUtil;
import com.diboot.core.util.BeanUtils;


import com.hznc.wcs.entity.FormDesign;
import com.hznc.wcs.entity.bpm.BpmCategory;
import com.hznc.wcs.entity.bpm.BpmProcessDefinitionInfo;
import com.hznc.wcs.util.BpmnModelUtils;
import com.hznc.wcs.vo.bpm.BpmProcessDefinitionRespVO;
import org.flowable.bpmn.model.BpmnModel;
import org.flowable.bpmn.model.UserTask;
import org.flowable.common.engine.impl.db.SuspensionState;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.repository.ProcessDefinition;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget;
import org.mapstruct.factory.Mappers;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Bpm 流程定义的 Convert
 */
@Mapper
public interface BpmProcessDefinitionConvert {

    BpmProcessDefinitionConvert INSTANCE = Mappers.getMapper(BpmProcessDefinitionConvert.class);

    default List<BpmProcessDefinitionRespVO> buildProcessDefinitionList(List<ProcessDefinition> list,
                                                                        Map<String, Deployment> deploymentMap,
                                                                        Map<String, BpmProcessDefinitionInfo> processDefinitionInfoMap,
                                                                        Map<String, FormDesign> formMap,
                                                                        Map<String, BpmCategory> categoryMap) {
        return list.stream().map(definition -> {
            Deployment deployment = MapUtil.get(deploymentMap, definition.getDeploymentId(), Deployment.class);
            BpmProcessDefinitionInfo processDefinitionInfo = MapUtil.get(processDefinitionInfoMap, definition.getId(), BpmProcessDefinitionInfo.class);
            FormDesign form = null;
            if (processDefinitionInfo != null) {
                form = MapUtil.get(formMap, processDefinitionInfo.getFormId(), FormDesign.class);
            }
            BpmCategory category = MapUtil.get(categoryMap, definition.getCategory(), BpmCategory.class);
            return buildProcessDefinition(definition,deployment,processDefinitionInfo,form,category,null,null);
        }).collect(Collectors.toList());
    }

    default BpmProcessDefinitionRespVO buildProcessDefinition(ProcessDefinition definition,
                                                              Deployment deployment,
                                                              BpmProcessDefinitionInfo processDefinitionInfo,
                                                              FormDesign form,
                                                              BpmCategory category,
                                                              BpmnModel bpmnModel,
                                                              List<UserTask> startUserSelectUserTaskList) {
        BpmProcessDefinitionRespVO respVO = BeanUtils.convert(definition, BpmProcessDefinitionRespVO.class);
        respVO.setSuspensionState(definition.isSuspended() ? SuspensionState.SUSPENDED.getStateCode() : SuspensionState.ACTIVE.getStateCode());
        // BpmProcessDefinitionInfoDO
        if (processDefinitionInfo != null) {
            BeanUtils.copyProperties(processDefinitionInfo, respVO);
        }
        // Deployment
        if (deployment != null) {
            respVO.setDeploymentTime(LocalDateTimeUtil.of(deployment.getDeploymentTime()));
        }
        // Form
        if (form != null) {
            respVO.setFormName(form.getFormName());
        }
        // Category
        if (category != null) {
            respVO.setCategoryName(category.getName());
        }
        // BpmnModel
        if (bpmnModel != null) {
            respVO.setBpmnXml(BpmnModelUtils.getBpmnXml(bpmnModel));
            respVO.setStartUserSelectTasks(BeanUtils.convertList(startUserSelectUserTaskList, BpmProcessDefinitionRespVO.UserTask.class));
        }
        return respVO;
    }

    @Mapping(source = "from.id", target = "to.id", ignore = true)
    void copyTo(BpmProcessDefinitionInfo from, @MappingTarget BpmProcessDefinitionRespVO to);

}
